const { Transform } = require("stream");
const utils = require('../../utils');

module.exports = function(res) {
  const stream = new Transform({ objectMode: true });
  const list = [];
  stream._transform = function (chunk, encoding, callback) {
    utils.debug('responseWriter [chunk]:', chunk);
    stream.push(chunk);
    list.push(chunk);
    callback();
  };
  
  stream.on('finish', function () {
    res.send({ success: true, list });
    utils.debug('responseWriter [finish]');
  });

  return stream;
};